package com.gscitysfy.cus.drain.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gscitysfy.cus.drain.modules.command.dto.DangerSituationManageDto;
import com.gscitysfy.cus.drain.modules.command.entity.DangerSituationManage;
import com.gscitysfy.cus.drain.modules.command.mapper.DangerSituationManageMapper;
import com.gscitysfy.cus.drain.modules.command.service.IDangerSituationManageService;
import com.gscitysfy.cus.drain.modules.warning.entity.DrainMonitorMnAlarm;
import com.gscitysfy.cus.drain.modules.warning.mapper.DrainMonitorMnAlarmMapper;
import com.gscitysfy.cus.drain.modules.warning.service.IDrainMonitorMnAlarmService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@EnableScheduling()
public class alarmToSitulationTask {
    private static final Logger logger = LoggerFactory.getLogger(alarmToSitulationTask.class);
    @Autowired
    DrainMonitorMnAlarmMapper drainMonitorMnAlarmMapper;
    @Autowired
    DangerSituationManageMapper dangerSituationManageMapper;
    @Autowired
    IDangerSituationManageService dangerSituationManageService;
    @Scheduled(cron = "0 0/5 * * * ?")
    public void jobDeal(){
        //查询今日的报警，实时同步
        logger.info("同步报警监测数据开始=======================================");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DrainMonitorMnAlarm DrainMonitorMnAlarm = new DrainMonitorMnAlarm();
        DrainMonitorMnAlarm.setIsJobQuery("1");//表示通过预警生成
        List<DrainMonitorMnAlarm> drainMonitorMnAlarmList =  drainMonitorMnAlarmMapper.getPageList(DrainMonitorMnAlarm);
        logger.info("同步报警监测数据list"+drainMonitorMnAlarmList.size());

        if(CollectionUtils.isNotEmpty(drainMonitorMnAlarmList)){
            for(DrainMonitorMnAlarm drainMonitorMnAlarm: drainMonitorMnAlarmList){
                //将 drainMonitorMnAlarm 的数据放到   DangerSituationManageDto 中
                //获取id查询alarmId 是否已经存在
                LambdaQueryWrapper<DangerSituationManage> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.eq(DangerSituationManage::getAlarmId,drainMonitorMnAlarm.getCmKid());
                List<DangerSituationManage> dangerSituationManageList = dangerSituationManageService.list(queryWrapper);
                //查询看看是否数据库已经存在，且包括逻辑删除，如果存在，则需要重新同步，如果已经逻辑删除，则可以不管
                if(CollectionUtils.isEmpty(dangerSituationManageList)){
                    DangerSituationManageDto dangerSituationManageDto = new DangerSituationManageDto();
                    dangerSituationManageDto.setCmCode(currentDateCode());
                    sdf.format(drainMonitorMnAlarm.getAlarmTime());
                    String xqName =sdf.format(drainMonitorMnAlarm.getAlarmTime())+drainMonitorMnAlarm.getTagName()+"发生"+drainMonitorMnAlarm.getAlarmLevel()+"级险情";
                    dangerSituationManageDto.setName(xqName);
                    dangerSituationManageDto.setAddress(drainMonitorMnAlarm.getAddress());
                    dangerSituationManageDto.setPondingDepth(drainMonitorMnAlarm.getAlarmValue());
                    dangerSituationManageDto.setDiscoveryTime(sdf1.format(drainMonitorMnAlarm.getAlarmTime()) );
                    dangerSituationManageDto.setPublishStatus("0");//草稿
                    dangerSituationManageDto.setAlarmId(drainMonitorMnAlarm.getCmKid());
                    dangerSituationManageDto.setLongitude(drainMonitorMnAlarm.getLongitude());
                    dangerSituationManageDto.setLatitude(drainMonitorMnAlarm.getLatitude());
                    dangerSituationManageDto.setCmRegflag(drainMonitorMnAlarm.getRegion());
                    dangerSituationManageDto.setSource("xtbj");
                    dangerSituationManageService.alarmGenerate(dangerSituationManageDto);
                    logger.info("保存成功,险情名称："+xqName);

                }else{
                    DangerSituationManage dangerSituationManage = dangerSituationManageList.get(0);
                    if(!dangerSituationManage.getCmUsable()||!dangerSituationManage.getDispatchFlag()||"0".equals(dangerSituationManage.getPublishStatus())){
                        //true表示删除,不删除，或者发布状态不是已发布，或者派单状态不是已派单需要比对数据，对已有的数据是已解除的进行逻辑删除
                        if("1".equals(drainMonitorMnAlarm.getAuditResultCode())){
                            dangerSituationManage.setCmUsable(true);
                            dangerSituationManage.setDescribe(drainMonitorMnAlarm.getAuditResultDesc());
                            dangerSituationManageService.updateById(dangerSituationManage);
                        }
                    }
                }
            }
        }
    }
    public String  currentDateCode(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String currentCode = sdf.format(date);

        int max=10000;
        int min=1000;
        Random random = new Random();
        String randomMath =  (random.nextInt(max - min) + min + 1)+"";
        String currentDateCode = "XTXQ"+currentCode+randomMath;
        return currentDateCode;
    }
}
